package fr.epsi.services;


import fr.epsi.studentsmaps.MenuActivity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class LocalisationService extends Service
{
 
		private LocationManager	lManager = null;
		public static String chaineLoc = "Defaut";
		private String choix_source = "";
		
		
		private LocationListener onLocationChange = new LocationListener()
		{

			 @Override
			 public void onStatusChanged(String provider, int status, Bundle extras)
			 {
			 }
	
			 @Override
			 public void onProviderEnabled(String provider)
			 {
			 }
	
			 @Override
			 public void onProviderDisabled(String provider)
			 {
			 }
	
			 @Override
			 public void onLocationChanged(Location location)
			 {
			
				 Log.i("BPL", "Localisation");
				 
				 
				 /*
				Double latitude = location.getLatitude();
				 Double longitude = location.getLongitude();
				 
				 */
				 
				 LocalisationService.chaineLoc =

			  			  location.getLatitude()
			  			  +"#"+location.getLongitude()
			  			  +"#"+location.getAltitude()
			  			  ;
				 
				 
				 if(!MenuActivity.isLocated)
				 MenuActivity.loadLocalisation.sendEmptyMessage(3);
				 
				 
				 /*
				 UserActivity.latitude = location.getLatitude();
				 UserActivity.longitude = location.getLongitude();
				 UserActivity.altitude = location.getAltitude();
				*/
				 
				
				 
				 
				 
				 
				 
				 /*
				 
				BearLocalisationService.chaineLoc =

			  			   "#" // CP
			  			  +"#" // VILLE
			  			  +"#" // region
			  			  +"#" // PAYS
			  			  +"#"+location.getLatitude()
			  			  +"#"+location.getLongitude()
			  			  +"#"+location.getAltitude()
			  			  +"#"+"#"+"#";
			  			  
			  			  */
			 }
		};

		 @Override
		 public IBinder onBind(Intent arg0)
		 {
			 return null;
		 }

		 @Override
		 public void onCreate()
		 {
			 
			 Log.i("LocalisationS","OK Loc");
			 
			 
			 lManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
			 lManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 0, onLocationChange);
			 lManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
			 Criteria crit = new Criteria();
		     crit.setAccuracy(Criteria.ACCURACY_FINE);
		   	 choix_source = lManager.getBestProvider(crit, true);
		   	 
		   	 
		   	 Log.e("Loc","Provider : "+choix_source);
	    	 lManager.requestLocationUpdates(choix_source,200,0, onLocationChange);
	    	 
			 
			 /*
			 lManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
				if (lManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
					lManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0,onLocationChange);
				lManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 0, onLocationChange);
				*/
	    	 
			   	
	    	 super.onCreate();
		 }
		

		 
		 @Override
		 public int onStartCommand(Intent intent, int flags, int startId)
		 {
		
			 return super.onStartCommand(intent, flags, startId);
		 }
		
		 @Override
		 public void onDestroy()
		 {
			 lManager.removeUpdates(onLocationChange);
			 
			 super.onDestroy();
		 }


}